package org.springside.examples.showcase.schedule;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.examples.showcase.wb.entity.KvNode;
import org.springside.examples.showcase.wb.entity.TopKeyWord;
import org.springside.examples.showcase.wb.entity.WbContentRet;
import org.springside.examples.showcase.wb.entity.WbEvent;
import org.springside.examples.showcase.wb.service.WbContentManager;

/**
 * 被Spring的Quartz MethodInvokingJobDetailFactoryBean定时执行的普通Spring Bean.
 */
public class QuartzJob {
	private static Logger logger = LoggerFactory.getLogger(QuartzJob.class);
	private WbContentManager wbContentManager;

	/**
	 * 定时调度，生成微博热点
	 */
	public void execute() {
		String pardate = "2012-02-01";
		List<TopKeyWord> topKeyWords = wbContentManager.getTopKeyWords(pardate);
		if (topKeyWords.size() == 0) {
			// 1.需要分词的热转微博
			List<WbContentRet> needSegmentationFeeds = wbContentManager.getNeedSegmentationFeeds(pardate);
			// 2.统计词频
			Map<String, KvNode> statKeywordFreq = wbContentManager.statKeywordFreq(needSegmentationFeeds);
			// 3.按权重排序
			List<KvNode> sortList = SortByWeight(statKeywordFreq);
			statKeywordFreq = null;

			for (Iterator<KvNode> iterator = sortList.iterator(); iterator.hasNext();) {
				KvNode kvNode = iterator.next();
				String[] mids = kvNode.getInfo().split("\\,");
				if (mids.length < 2 || (kvNode.getValue() / kvNode.getInfo().split("\\,").length) < 200) {
					iterator.remove();
					continue;
				}

				System.out.println(kvNode.getKey() + ":" + (kvNode.getValue() / kvNode.getInfo().split("\\,").length) + ":" + kvNode.getValue() + ":" + kvNode.getInfo());
			}
			// 保存每条微博的分词，尽可能全
			wbContentManager.saveWbKeywords(needSegmentationFeeds, sortList, pardate);
			needSegmentationFeeds = null;

			System.out.println("---------------------------------");
			for (Iterator<KvNode> iterator = sortList.iterator(); iterator.hasNext();) {
				KvNode kvNode = iterator.next();
				if ((kvNode.getValue() / kvNode.getInfo().split("\\,").length) < 300 || kvNode.getKey().contains(":")) {
					iterator.remove();
					continue;
				}

				System.out.println(kvNode.getKey() + ":" + (kvNode.getValue() / kvNode.getInfo().split("\\,").length) + ":" + kvNode.getValue() + ":" + kvNode.getInfo());
			}

			// 6.进一步去杂质
			for (Iterator<KvNode> iterator = sortList.iterator(); iterator.hasNext();) {
				long bt = System.currentTimeMillis();
				KvNode kvNode = iterator.next();
				int count = 0;
				KvNode kvNode2 = null;
				String info = null;
				String info2 = null;
				List<String> mids = null;
				List<String> mids2 = null;
				List<String> _mids2 = null;
				for (Iterator<KvNode> iterator2 = sortList.iterator(); iterator2.hasNext();) {
					kvNode2 = iterator2.next();
					info = kvNode.getInfo();
					info2 = kvNode2.getInfo();
					mids = Arrays.asList(info.split("\\,"));
					mids2 = Arrays.asList(info2.split("\\,"));
					_mids2 = new ArrayList<String>(mids2);
					_mids2.retainAll(mids);
					int maxsize = mids2.size() > mids.size() ? mids2.size() : mids.size();
					if (kvNode != kvNode2 && info.equalsIgnoreCase(info2) && kvNode2.getKey().contains(kvNode.getKey())) {
						// 市政府 : 746 : 1493 : 3408299862691328,3408255474455403
						// 市政 : 746 : 1493 : 3408299862691328,3408255474455403
						// 干什 : 877 : 1754 : 3408275429464813,3408397796070148
						// 干什么 : 877 : 1754 : 3408275429464813,3408397796070148
						count = 0;
						break;
					} else if (kvNode != kvNode2 && info.equalsIgnoreCase(info2) || kvNode != kvNode2 && (_mids2.size() * 2 > maxsize)) {
						// 的话 : 1149 : 2299 : 3408236717484642,3408271979638603 其他分词中没有3408236717484642,3408271979638603，即该词没有意义
						// 以后 : 742 : 1485 : 3408299862691328,3407937009916859
						// 不是 : 463 : 2783 : 3408299862691328,3408048213381779,3407937009916859,3408007835111674,3407961173435006,3408231747746910
						// 不要 : 444 : 3113 : 3408299862691328,3408018337342397,3408018563311166,3408003250781521,3407937009916859,3408296326837312,3407324339552362
						count++;
						if (count > 1) {
							break;
						}
					}
				}
				if (count < 2) {
					iterator.remove();
					continue;
				}
				System.out.println(kvNode.getKey() + ":" + (kvNode.getValue() / kvNode.getInfo().split("\\,").length) + ":" + kvNode.getValue() + ":" + kvNode.getInfo() + "时间：" + (System.currentTimeMillis() - bt));
			}

			// 保存最热频词
			List<TopKeyWord> topKeywords = wbContentManager.saveKeywords(pardate, sortList);
			sortList = null;

			Collections.sort(topKeywords, new Comparator<TopKeyWord>() {
				@Override
				public int compare(TopKeyWord o1, TopKeyWord o2) {
					return o2.getRef_mid().split("\\,").length - o1.getRef_mid().split("\\,").length;
				}
			});
			for (TopKeyWord topKeyWord : topKeywords) {
				System.out.println(topKeyWord.getKeyword() + ":" + topKeyWord.getRef_mid());
			}

			List<WbEvent> wbEvents = new ArrayList<WbEvent>();
			for (TopKeyWord topkw : topKeywords) {
				String ref_mid = topkw.getRef_mid();
				Set<String> ref_midsSet1 = new HashSet<String>();
				ref_midsSet1.addAll(Arrays.asList(ref_mid.split("\\,")));
				boolean condition = true;
				String ref_mids = null;
				Set<String> ref_midsSet2 = null;
				for (WbEvent wbEvent : wbEvents) {
					ref_mids = wbEvent.getRef_mids();
					ref_midsSet2 = new HashSet<String>();
					ref_midsSet2.addAll(Arrays.asList(ref_mids.split("\\,")));
					Set<String> ref_midsSet3 = new HashSet<String>();
					ref_midsSet3.addAll(Arrays.asList(ref_mids.split("\\,")));
					ref_midsSet3.retainAll(ref_midsSet1);
					int min = ref_midsSet2.size() < ref_midsSet1.size() ? ref_midsSet2.size() : ref_midsSet1.size();
					if ((min == 2 && ref_midsSet2.containsAll(ref_midsSet1)) || ref_midsSet3.size() * 2 > min) {
						condition = false;
						String[] kww = (wbEvent.getDetail() + topkw.getKeyword() + ":" + topkw.getWeight()).split("\\,");
						Arrays.sort(kww, new Comparator<String>() {
							@Override
							public int compare(String o1, String o2) {
								String[] w1 = o1.split(":");
								String[] w2 = o2.split(":");
								if (w1.length == 2 && w2.length == 2) {
									return Integer.parseInt(w2[1]) - Integer.parseInt(w1[1]);
								} else {
									return 0;
								}
							}
						});
						String detail = "";
						String eventName = "";
						for (String item : kww) {
							detail += item + ",";
							eventName += item.split(":")[0] + ",";
						}
						ref_midsSet2.addAll(ref_midsSet1);
						ref_mids = "";
						for (String mid : ref_midsSet2) {
							ref_mids += mid + ",";
						}
						wbEvent.setDetail(detail);
						wbEvent.setWeight(wbEvent.getWeight() + topkw.getWeight());
						wbEvent.setEventName(eventName);
						wbEvent.setRef_mids(ref_mids);
					}
				}

				if (condition) {
					WbEvent wbEvent = new WbEvent();
					wbEvent.setDetail(topkw.getKeyword() + ":" + topkw.getWeight() + ",");
					wbEvent.setEventName(topkw.getKeyword() + ",");
					wbEvent.setRef_mids(topkw.getRef_mid());
					wbEvent.setWeight(topkw.getWeight());
					wbEvents.add(wbEvent);
				}
			}

			for (WbEvent wbEvent : wbEvents) {
				System.out.println(wbEvent.getWeight() + ":" + wbEvent.getDetail());
			}
		}
	}

	private List<KvNode> SortByWeight(Map<String, KvNode> statKeywordFreq) {
		List<KvNode> sortList = new ArrayList<KvNode>();
		sortList.addAll(statKeywordFreq.values());
		// 此处很有意思，权重/贡献
		Collections.sort(sortList, new Comparator<KvNode>() {
			@Override
			public int compare(KvNode o1, KvNode o2) {
				int k1 = o1.getInfo().split("\\,").length;
				int k2 = o2.getInfo().split("\\,").length;
				return (o2.getValue() / k2 - o1.getValue() / k1);
			}
		});
		return sortList;
	}

	@Autowired
	public void setWbContentManager(WbContentManager wbContentManager) {
		this.wbContentManager = wbContentManager;
	}

}
